#!/bin/bash -e

log=.log.`date +%N`
echo `basename $0` $@ >>$log || exit 0;
prog=`basename $0`
args=$@

function info() {
cat /mnt/ilustre/app/medical/tools/script/info.snp.txt
rm $log
exit 65
}

function error() {
echo `basename $0`': invalid option -?'
info
}

while getopts  ":a:s:i:l:d:gkLGnc:Ap:m:q:r:t:vF:D:" opts
do
        case  $opts  in
		a) cutx=$OPTARG;;
        i)
			interval=$OPTARG
			i=i
		;;
		l)
			target=$OPTARG
			f=f
		;;
		g) single_cell=T;;
		G) somatic=T;;
		d) desti=$OPTARG;;
		k) mark=T;;
		n)
			cutadaptor=$OPTARG
			n=n
		;;
		c)
			cnv=$OPTARG
			c=c
		;;
		r) realn_interval=$OPTARG;;
		m)
			mem=$OPTARG
			m=m
		;;
		p)
			cpu=$OPTARG
			p=p
		;;
		q)
			query=$OPTARG
			q=q
		;;
		A)
			a=n
		;;
		s) sample_name=$OPTARG;;
		L) run_local=T;;
		t) continue_dir=$OPTARG;;
		v) cov=T;;
        F) depth_threshhold=$OPTARG;;
        D) depth_design=$OPTARG;;
		h) info;;
		\?) error;;
        esac
done
shift $(($OPTIND - 1))

if [ $# -lt 2 ]; then info; fi

. $var

mnt=mnt.sh
path_abs=path_abs.sh
makedir=makedir.sh
sam2b=sam2b_samtools.sh
filter_bam_gatk=filter_bams_gatk.sh
filter_bam_gatk_suf=filter_gatk
merge_bam=merge_bam.sh
deldup=deldup.sh
methy_anno="methy_anno_170216.sh"
summ=summ.sh
summ_gatk=summ_gatk.sh
fastqc='fastqc.sh -t8 -s$sample_name'
fqstat='fqstat.sh -t8 -s$sample_name'
csv2xls=csv2xls.pl
tab2xls=tab2xls.pl
bk='bk.sh -tproject/$destination'

cal0=ugs.sh
anno=anno_110416.sh


if [ "$interval" = "brca12" ]; then
	cutadp=cutadp_170214.sh
	target=$data_path/intervals/1/brca.qiagen.NGHS-102X-Covered.b37.bed
elif [ "$interval" = "brca44" ]; then
	cutadp=cutadp_170214.sh
	target=$data_path/intervals/1/brca.qiagen.NGHS-001X-Covered.b37.bed
elif [ "$interval" = "ctc80" ]; then
	cutadp=cutadp_ctc80_discard_trimmed.sh
	target=$data_path/intervals/1/ctc80.bed
elif [ "$interval" = "brca12mor" ]; then
	cutadp=cutadp_ph_miseq.sh
	target=$data_path/intervals/1/brca12_mor.bed
elif [ "$interval" = "brca12ph" ]; then
	cutadp=cutadapt_brca12_ph.sh
	target=$data_path/intervals/1/brca12_b37_sort_ph.bed
elif [ "$interval" = "ca50mor" ]; then
	cutadp=cutadp_ph_miseq.sh
	target=$data_path/intervals/1/ca50_mor.bed
elif [ "$interval" = "exomes" ]; then
	target=$data_path/intervals/1/b37.agilent_Exon_V5_UTRs.bed
elif [ "$interval" = "wg" ]; then
	target=$data_path/intervals/1/b37.bed
elif [ "$interval" = "tmp" ]; then
	target=$data_path/intervals/1/probe3.b37.pade0.new.bed
else
	cutadp=cutadp_170214.sh
fi

echo interval: $interval


# samtools_path=/mnt/ilustre/app/rna/bin #special version needed
java=$tools_path/jre1.6.0_45/bin/java
# java_run1="$java $java_mem -jar $tools_path/jars" #specific version needed

for bam; do
bams="$bams $(path_abs.sh $bam)"
done

echo bams: $bams


if test -z "$run_continue_dir"; then
run_continue_dir=`$makedir -p$sample_name` && cd $run_continue_dir
else 
cd $run_continue_dir
fi

# ------------- end makedir ------------------



$mnt $$ `basename $0 .sh`.`basename $(pwd)` &
mnt_pid=$!


#--------------------------------------------------------

# calling 

if
cat $pid_log|grep "$cal0 $bams done" >/dev/null; then
echo $cal0 $bams done
else
	if test -n "$somatic"; then
		echo somatic or low coverage
        if test -n "$single_cell"; then
            echo single cell
            $cal0 -G1 -p$out_prefix -i$target -d${depth_design:=30} $bams
        else
            $cal0 -G -p$out_prefix -i$target -d${depth_design:=30} $bams
		fi
	else
        if test -n "$single_cell"; then
            $cal0 -1 -p$out_prefix -i$target -d${depth_design:=30} $bams
        else
            $cal0 -p$out_prefix -i$target -d${depth_design:=30} $bams
        fi
	fi
fi

# anno

if
cat $pid_log|grep "$anno $out_prefix.snp.vcf $out_prefix.indel.vcf done" >/dev/null; then
echo $anno done
else

if test -n "$somatic"; then
	echo somatic
	if test -n "$single_cell"; then
		echo;echo;echo single_cell
		$anno -m -g -l $target $out_prefix.snp.vcf $out_prefix.indel.vcf
	else
		$anno -m -l $target $out_prefix.snp.vcf $out_prefix.indel.vcf
	fi
else
	echo germline
	if test -n "$single_cell"; then
		echo;echo;echo single_cell
		$anno -g -l $target $out_prefix.snp.vcf $out_prefix.indel.vcf
	else
		$anno -l $target $out_prefix.snp.vcf $out_prefix.indel.vcf
	fi
fi

fi

# format_simple=format_top_simple.sh
# if
# cat $pid_log|grep "$format_simple done" >/dev/null; then
# echo $format_simple done
# else
# format_top_simple.sh 
# fi

# format=format_top_0222.sh
# if
# cat $pid_log|grep "$format $out_prefix.high.vcf $out_prefix.low.vcf done" >/dev/null; then
# echo $format done
# else
# format_top_0222.sh $out_prefix.high.vcf $out_prefix.low.vcf
# fi


# rename=file_rename_0222.sh
# if
# cat $pid_log|grep "$rename $sample_name done" >/dev/null; then
# echo $rename done
# else
# file_rename_0222.sh $sample_name
# fi



# echo;echo; echo desti: $desti
# bk.sh -tproject/$desti ${sample_name}*variant*.xls \
# ${sample_name}*simple_info*.xls \
# ${sample_name}*summary*.xls \
# *stat*.xls \
# *.pdf \
# ${sample_name}*variants*.html \
# ${sample_name}*deleterious.txt \
# ${sample_name}*impact*drugs-prediction* \
# .log* .pig.log

# bk_zip.sh ${sample_name}*variant*.xls \
# ${sample_name}*simple_info*.xls \
# ${sample_name}*summary*.xls \
# *stat*.xls \
# *.pdf \
# ${sample_name}*variants.2.html \
# ${sample_name}*impact*drugs-prediction.html



. $cmd_done